package com.example.employeemanagementsystem.controller;

import com.example.employeemanagementsystem.model.Employee;
import com.example.employeemanagementsystem.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/add")
    public ResponseEntity<?> addEmployee(@RequestBody Employee employee) {
        boolean isAdded = employeeService.addEmployee(employee);
        return ResponseEntity.ok(Collections.singletonMap("success", isAdded));
    }

    @PutMapping("/edit/{employeeNumber}")
    public ResponseEntity<?> editEmployee(@PathVariable String employeeNumber, @RequestBody Employee employee) {
        boolean isUpdated = employeeService.updateEmployee(employeeNumber, employee);
        return ResponseEntity.ok(Collections.singletonMap("success", isUpdated));
    }

    @DeleteMapping("/delete/{employeeNumber}")
    public ResponseEntity<?> deleteEmployee(@PathVariable String employeeNumber) {
        boolean isDeleted = employeeService.deleteEmployee(employeeNumber);
        return ResponseEntity.ok(Collections.singletonMap("success", isDeleted));
    }

    @GetMapping("/list")
    public List<Employee> getEmployees() {
        return employeeService.getAllEmployees();
    }
}